﻿namespace NEvoWeb.Modules.NB_Store
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Specialized;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public class FormTemplateBuilder
    {
        private string _Title = "";
        private NameValueCollection Displays = new NameValueCollection();
        private NameValueCollection Inputs = new NameValueCollection();

        public void AddControl(object ctrl, string DisplayLabel)
        {
            string id = Conversions.ToString(Operators.ConcatenateObject(NewLateBinding.LateGet(ctrl, null, "ID", new object[0], null, null, null), this.Inputs.Keys.Count.ToString()));
            this.AddControl(id, RuntimeHelpers.GetObjectValue(ctrl), DisplayLabel);
        }

        public void AddControl(string id, object ctrl, string DisplayLabel)
        {
            string str = this.getFieldTemplate(id, RuntimeHelpers.GetObjectValue(ctrl));
            if (str != "")
            {
                this.Inputs.Add(id, str);
                this.Displays.Add(id, DisplayLabel);
            }
        }

        public void AddTitle(string Title)
        {
            this.AddTitle(Title, false);
        }

        public void AddTitle(string Title, bool IsHTML)
        {
            if (IsHTML)
            {
                this._Title = Title;
            }
            else
            {
                this._Title = "<h1>" + Title + "</h1><hr/>";
            }
        }

        public string getFieldTemplate(object ctrl)
        {
            return this.getFieldTemplate(Conversions.ToString(NewLateBinding.LateGet(ctrl, null, "id", new object[0], null, null, null)), RuntimeHelpers.GetObjectValue(ctrl));
        }

        public string getFieldTemplate(string id, object ctrl)
        {
            string str2 = "[<tag ";
            if (ctrl is NBright_TextBox)
            {
                str2 = str2 + " type=\"textbox\"";
            }
            else if (ctrl is NBright_dateEditControl)
            {
                str2 = str2 + " type=\"dateeditcontrol\"";
            }
            else if (ctrl is NBright_DropDownList)
            {
                str2 = str2 + " type=\"dropdownlist\"";
            }
            else if (ctrl is NBright_CheckBox)
            {
                str2 = str2 + " type=\"checkbox\"";
            }
            else if (ctrl is NBright_RadioButtonList)
            {
                str2 = str2 + " type=\"radiobuttonlist\"";
            }
            else if (ctrl is RequiredFieldValidator)
            {
                str2 = str2 + " type=\"rfvalidator\"";
            }
            else if (ctrl is RegularExpressionValidator)
            {
                str2 = str2 + " type=\"revalidator\"";
            }
            else if (ctrl is ValidationSummary)
            {
                str2 = str2 + " type=\"validationsummary\"";
            }
            else
            {
                return "";
            }
            bool flag = false;
            foreach (PropertyInfo info in ctrl.GetType().GetProperties())
            {
                string str3 = "";
                if (info.GetValue(RuntimeHelpers.GetObjectValue(ctrl), null) != null)
                {
                    str3 = info.GetValue(RuntimeHelpers.GetObjectValue(ctrl), null).ToString();
                }
                if (str3 != "")
                {
                    if (info.Name.ToLower() == "id")
                    {
                        str2 = str2 + " " + info.Name + "=\"" + id + "\"";
                        flag = true;
                    }
                    else
                    {
                        str2 = str2 + " " + info.Name + "=\"" + str3 + "\"";
                    }
                }
            }
            if (!flag)
            {
                str2 = str2 + " id=\"" + id + "\"";
            }
            return (str2 + " />]");
        }

        public string GetFormTemplate()
        {
            int num=0;
            string str2 = this._Title + "<table>";
            int num3 = num + 1;
            int num2 = this.Inputs.Keys.Count - 1;
            for (num = 0; ((num3 >> 0x1f) ^ num) <= ((num3 >> 0x1f) ^ num2); num += num3)
            {
                str2 = (((str2 + "<tr>" + "<td align=\"right\">") + this.Displays[this.Displays.Keys[num]] + "</td>") + "<td>" + this.Inputs[this.Inputs.Keys[num]]) + "</td>" + "</tr>";
            }
            return (str2 + "</table>");
        }
    }
}

